from rest_framework import serializers

from bowen.models import Bowen


class HouTaiSerializer(serializers.ModelSerializer):
    fenlei = serializers.SerializerMethodField()
    class Meta:
        model = Bowen
        fields = ('id','title','postdate','fenlei','imgurl','zishu')

    def get_fenlei(self, obj):
        # 获取fenlei字段的选项描述
        return dict(Bowen.fenlei_choices).get(obj.fenlei, '2')


class HouTaiContentSerializer(serializers.ModelSerializer):
    fenlei_display = serializers.SerializerMethodField()
    class Meta:
        model = Bowen
        fields = ('id','title','postdate','content','fenlei','imgurl','fenlei_display','zishu')

    def get_fenlei_display(self, obj):
        # 获取fenlei字段的选项描述
        return dict(Bowen.fenlei_choices).get(obj.fenlei, '2')

    # def create(self,validated_data):
    #     print(validated_data)
    #     content_data = validated_data.pop('content')
    #     content = Detail.objects.create(**content_data)
    #     validated_data.pop('fenlei_display')
    #     bowen_data = validated_data
    #     bowen_data['content_id'] = content.id
    #     bowen = Bowen.objects.create(**bowen_data)
    #     return bowen
